#!/usr/bin/env node

const rd = require('rd');
const fs = require('fs')
const convert = require('xml-js');

let profiles = JSON.parse(
    convert.xml2json(`<profiles>
        <!--本地开发环境-->
        <profile>
            <id>dev</id>
            <properties>
                <profiles.active>dev</profiles.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <!--测试环境-->
        <profile>
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>
        </profile>
        <!--生产环境-->
        <profile>
            <id>prod</id>
            <properties>
                <profiles.active>prod</profiles.active>
            </properties>
        </profile>
    </profiles>
`,
        {
            compact: true,
            spaces: 4
        }));
let properties = JSON.parse(
    convert.xml2json(`<properties><maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding></properties>`,
        {
            compact: true,
            spaces: 4
        }));

let resource = JSON.parse(convert.xml2json(`<resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>dev/*</exclude>
                    <exclude>prod/*</exclude>
                    <exclude>test/*</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources/${profiles.active}</directory>
            </resource>
   </resources>   `, {compact: true, spaces: 4}));


// 同步列出目录下的所有文件
let files = rd.readSync('F:\\dev\\gaiay\\all');
for (let f of files) {
    if (f.endsWith("xml") && f.indexOf('.idea') < 0) {
        let content = fs.readFileSync(f, "utf-8");
        if (content.indexOf("<packaging>war</packaging>") > 1) {
            let json = JSON.parse(convert.xml2json(content, {compact: true, spaces: 4}));
            //////////////////////////////////
            if (!json.project.profiles) {
                json.project.profiles = {};
            }
            for (let p in  profiles.profiles) {
                json.project.profiles[p] = profiles.profiles[p];
            }

            //////////////////////////////////
            if (!json.project.properties) {
                json.project.properties = {};
            }
            for (let p in  properties.properties) {
                json.project.properties[p] = properties.properties[p];
            }
            /////////////////////////////////////////
            //////////////////////////////////////////
            let dependencies = json.project.dependencies;
            delete json.project.dependencies;
            json.project.dependencies = dependencies;
            ////////////////////////////////////////////
            if (!json.project.build) {
                json.project.build = {};
            }
            if (!json.project.build.plugins) {
                json.project.build.plugins = {};
            }
            if (!json.project.build.resources) {
                json.project.build.resources = {};
            }
            for (let p in resource.resources) {
                json.project.build.resources[p] = resource.resources[p];
            }
            /////////////////////////////
            //////////////////////////////////////////
            let build = json.project.build;
            delete json.project.build;
            json.project.build = build;

            ///////////////////////
            let options = {compact: true, ignoreComment: true, spaces: 4};
            let result = convert.json2xml(json, options);
            fs.writeFileSync(f, result);
            // console.log(f.replace("pom.xml", "pom_new.xml"));
        }
    }
}